import AJAX from '@/common/ajax'

export const GET_CM = 'GET_CM'
export const GET_SCORE = 'GET_SCORE'

export default {
  namespaced: true,
  state: {
    cm: {},
    score: {}
  },
  mutations: {
    [GET_CM](state, { data, params }) {
      state.cm[params.id] = data
      console.log('state.cm', state.cm[params.id])
    },
    [GET_SCORE](state, { data, params }) {
      state.score[params.id] = data
      console.log('state.cm', state.score[params.id])
    }
  },
  actions: {
    async getCm({ commit, state }, params) {
      const data = state.cm[params.id]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'comment',
          data: {
            id: params.id,
            sid: params.sid
          }
        })
        commit(GET_CM, data)
        return data
      }
    },
    async getScore({ commit, state }, params) {
      const data = state.score[params.id]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'love',
          data: {
            id: params.id,
            sid: params.sid
          },
          header: true
        })
        commit(GET_SCORE, {
          data,
          params
        })
        return data
      }
    },
    async mark({}, params) {
      let [err, data] = await AJAX({
        name: 'mark',
        data: {
          type: params.type,
          id: params.id,
          cid: params.cid
        },
        header: true
      })
      return data
    }
  }
}
